##########################################
## Copyright 2012-2013 Ceruti Francesco & contributors
##
## This file is part of LiSP (Linux Show Player).
##########################################

from PyQt4 import QtGui, QtCore
import lisp.layout as layout
from lisp.ui.qclicklabel import QClickLabel


class LayoutSelect(QtGui.QDialog):

    def __init__(self, parent=None):
        super(LayoutSelect, self).__init__(parent)

        self.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setWindowTitle('Layout selection')
        self.setMaximumSize(675, 375)
        self.setMinimumSize(675, 375)
        self.resize(675, 375)

        self.vLayout = QtGui.QVBoxLayout(self)
        self.vLayout.setMargin(5)

        self.gridLayoutWidget = QtGui.QWidget(self)
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setMargin(0)

        self.comboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.gridLayout.addWidget(self.comboBox, 0, 0, 1, 1)

        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)

        self.layButton = QtGui.QPushButton(self.gridLayoutWidget)
        self.layButton.setText('Select layout')
        self.layButton.setSizePolicy(sizePolicy)
        self.gridLayout.addWidget(self.layButton, 0, 1, 1, 1)

        self.vLayout.addWidget(self.gridLayoutWidget)

        self.description = QtGui.QTextBrowser(self)
        self.vLayout.addWidget(self.description)

        self.image = QtGui.QPushButton('Show image')
        self.vLayout.addWidget(self.image)

        for lay in layout.LAYOUT_LIST:
            self.comboBox.addItem(layout.getLayoutClass(lay).LAYOUT_DESCRIPTION_NAME)

        self.showDescription(0)

        self.comboBox.currentIndexChanged.connect(self.showDescription)
        self.layButton.clicked.connect(self.accept)
        self.image.clicked.connect(self.showFullImage)

    def closeEvent(self, e):
        e.ignore()

    def getSlected(self):
        return layout.LAYOUT_LIST[self.comboBox.currentIndex()]

    def showDescription(self, index):
        lay = layout.getLayoutClass(layout.LAYOUT_LIST[index])

        self.pixmap = lay.LAYOUT_DESCRIPTION_IMAGE
        self.description.setHtml('<center><h2>' + lay.LAYOUT_DESCRIPTION_NAME + '</h2></center><br>' + lay.LAYOUT_DESCRIPTION_TEXT)

    def showFullImage(self, p):
        fullImage = QtGui.QDialog(self)
        fullImage.setWindowModality(QtCore.Qt.ApplicationModal)
        fullImage.resize(self.pixmap.size())

        label = QClickLabel(fullImage)
        label.setPixmap(self.pixmap)
        label.clicked.connect(fullImage.accept)

        fullImage.exec_()
